/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.rpgmapper.gui;

import com.jme3.bounding.BoundingBox;
import com.jme3.font.BitmapFont;
import com.jme3.font.BitmapText;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
import com.jme3.scene.control.Control;
import com.jme3.ui.Picture;
import org.rpgmapper.JmeMain;
import org.rpgmapper.bo.Character;
import org.rpgmapper.bo.Stat;
import org.rpgmapper.bo.StatChangeListener;
import org.rpgmapper.tools.AppContext;

/**
 *
 * @author Nehon
 */
public class CharacterInfoControl extends AbstractControl implements StatChangeListener {

    private BitmapFont font;
    private BitmapFont fontSmall;
    private BitmapText nameText;
    private BitmapText nameText2;
    private BitmapText hpText;
    private BitmapText hpText2;
    private Vector3f sCoord = new Vector3f();
    private Picture deathQuad = new Picture("deathQuad");
    private float halfNameWidth = 0;
    private float halfHpWidth = 0;

    public CharacterInfoControl(Character character) {

        font = AppContext.getAssetManager().loadFont("Interface/Fonts/ImpactBig.fnt");
        fontSmall = AppContext.getAssetManager().loadFont("Interface/Fonts/ImpactSmall.fnt");
        nameText = new BitmapText(font);
        hpText = new BitmapText(fontSmall);
        nameText.setSize(font.getCharSet().getRenderedSize());

        hpText.setSize(fontSmall.getCharSet().getRenderedSize());
        nameText.setText(character.getName());
        hpText.setText(character.get(Stat.HitPoints) + "/" + character.get(Stat.MaxHitPoints));
        AppContext.getGuiNode().attachChild(nameText);
        AppContext.getGuiNode().attachChild(hpText);
        halfNameWidth = nameText.getLineWidth() * 0.5f;
        halfHpWidth = hpText.getLineWidth() * 0.5f;
        character.addStatChangeListener(this);

        nameText2 = new BitmapText(fontSmall);
        nameText2.setSize(font.getCharSet().getRenderedSize());
        nameText2.setText(nameText.getText());
        nameText2.setColor(ColorRGBA.DarkGray);


        hpText2 = new BitmapText(fontSmall);
        hpText2.setSize(fontSmall.getCharSet().getRenderedSize());
        hpText2.setText(hpText.getText());
        hpText2.setColor(ColorRGBA.DarkGray);


        AppContext.getGuiNode().attachChild(nameText2);
        AppContext.getGuiNode().attachChild(hpText2);

        deathQuad.setHeight(32);
        deathQuad.setWidth(32);
        deathQuad.setImage(AppContext.getAssetManager(), "Textures/Icons/Dead.png", true);

    }

    @Override
    protected void controlUpdate(float f) {

        sCoord.set(spatial.getWorldTranslation());
        sCoord.addLocal(0, ((BoundingBox) spatial.getWorldBound()).getYExtent() * 2, 0);
        AppContext.getRtsCam().getCamera().getScreenCoordinates(sCoord, sCoord);
        sCoord.y += 40;
        sCoord.x -= halfNameWidth;
        sCoord.z = 1;
        nameText.setLocalTranslation(sCoord);

        sCoord.y -= 1;
        sCoord.x += 2;
        sCoord.z = -1;
        nameText2.setLocalTranslation(sCoord);

        sCoord.y -= 20;
        sCoord.x += (halfNameWidth - halfHpWidth - 2);
        sCoord.z = 1;
        hpText.setLocalTranslation(sCoord);

        sCoord.y -= 1;
        sCoord.x += 2;
        sCoord.z = -1;
        hpText2.setLocalTranslation(sCoord);

        if (deathQuad.getParent() != null) {
            sCoord.y -= 25;
            sCoord.x += (halfHpWidth - 18);
            sCoord.z = 3;
            deathQuad.setLocalTranslation(sCoord);
        }
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
        // throw new UnsupportedOperationException("Not supported yet.");
    }

    public Control cloneForSpatial(Spatial sptl) {
        return null;
        // throw new UnsupportedOperationException("Not supported yet.");
    }

    public void onModifierChange(Character c, Stat stat) {
    }

    public void onBaseStatChange(Character c, Stat stat) {
    }

    public void onStatChange(Character c, Stat stat) {
        if (stat == Stat.MaxHitPoints || stat == Stat.HitPoints) {
            int hp = c.get(Stat.HitPoints);
            int hpMax = c.get(Stat.MaxHitPoints);
            hpText.setText(hp + " / " + hpMax);
            hpText.setColor(ColorRGBA.White);
            if (hp < (float) hpMax * 0.25f) {
                hpText.setColor(ColorRGBA.Yellow);
            }
            if (hp <= 0) {
                hpText.setColor(ColorRGBA.Orange);
            }
            int death = 1;
            if (c.getSide() > 0) {
                death = -c.get(Stat.Con);

            }
            if (hp < death) {
                hpText.setColor(ColorRGBA.Gray);
                AppContext.getGuiNode().attachChild(deathQuad);
            } else if (deathQuad.getParent() != null) {
                deathQuad.removeFromParent();
            }

            hpText2.setText(hpText.getText());
            halfHpWidth = hpText.getLineWidth() * 0.5f;
        }
    }

    public void dispose() {
        nameText.removeFromParent();
        nameText2.removeFromParent();
        hpText.removeFromParent();
        hpText2.removeFromParent();
        deathQuad.removeFromParent();
    }
}
